
<template>
  <div class="execution">
    <basic-container>
      <el-dialog
        :title="mode === 'create' ? '添加数据' : mode === 'edit' ? '编辑数据' : '预览数据'"
        :visible="!!mode"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :show-close="false"
        :destroy-on-close="true"	
        width="80%"
	
      >
        <Create
          v-if="mode === 'create'"
          @close="handleClosedDialog"
        />
        <Edit
          v-else-if="mode === 'edit'"
          :target="current"
          @close="handleClosedDialog"
        />
        <Edit
          v-else-if="mode === 'preview'"
          :target="current"
          :readonly="true"
          @close="handleClosedDialog"
        />
      </el-dialog>
      <avue-crud
        ref="crud"
        :page="page"
        :data="tableData"
        :permission="permissionList"
        :table-loading="tableLoading"
        :option="tableOption"
        @on-load="getList"
        @search-change="searchChange"
        @search-reset="searchReset"
        @refresh-change="refreshChange"
        @selection-change="selectionChange"
	:cell-style="cellStyle"
      >
        <template
          slot-scope="scope"
          slot="menu"
        >
          <el-button
            v-if="permissionList.editBtn"
            @click="handleEditClick(scope.row)"
            type="text"
            size="small"
          >编辑</el-button>
          <el-button
            @click="handlePreviewClick(scope.row)"
            type="text"
            size="small"
          >预览</el-button>
        </template>
        <template slot="menuRight">
          <el-button
            v-if="permissionList.addBtn"
            type="primary"
            size="small"
            @click="delBatchs()"
          >删除</el-button>
          <el-button
            v-if="permissionList.addBtn"
            type="primary"
            size="small"
            @click="handleCreateClick"
          >添加</el-button>
          <el-button
            type="primary"
            size="small"
            @click="dialogImportVisible = true"
          >导入</el-button>
          <el-button
            type="primary"
            size="small"
            @click="createFareDifference"
          >生成车站票款差异</el-button>
        </template>
      </avue-crud>
      <ImportData
        @importSuccess="searchReset"
        @close="dialogImportVisible = false"
        :dialogImportVisible="dialogImportVisible"
      />
      <open-dialog
        :DialogVisible.sync="editVisableDialog"
        title="生成车站票款差异"
        width="40%"
      >
        <el-form label-width="60px" v-if="editVisableDialog">
          <avue-form
            v-model="formCreate"
            :option="createFareOption"
            style="margin-bottom: 40px"
          ></avue-form>
          <div class="bottom-menu-noncash">
            <el-button type="primary" :loading="loading" @click="editFare()">生成</el-button>
            <el-button type="primary" @click="editVisableDialog = false"
              >取消</el-button
            >
          </div>
        </el-form>
      </open-dialog>
    </basic-container>
  </div>
</template>

<script>
import { fetchList, getObj, addObj, putObj, delObj ,createFares,delBatch} from '@/api/rca/swckbankcashpaymentnote';
import { tableOption,startDate, endDate } from '@/const/crud/rca/swckbankcashpaymentnote';
import { mapGetters } from 'vuex';
import Create from './components/Create.vue';
import Edit from './components/Edit.vue';
import ImportData from './components/ImportData.vue'

export default {
  name: 'swckbankcashpaymentnote',
  components: {
    Create,
    Edit,
    ImportData,
  },
  data () {
    return {
      formCreate: {},
      searchForm: {
        entryStartTime: [startDate, endDate],
        lineCode: '9'
      },
      tableData: [],
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 20 // 每页显示多少条
      },
      loading: false,
      tableLoading: false,
      tableOption: tableOption,

      uploading: false,
      current: null,
      mode: null,
      selection: [],
      // 上传
      dialogImportVisible: false,
      editVisableDialog: false,
    }
  },
  computed: {
    ...mapGetters(['permissions']),
    permissionList () {
      return {
        addBtn: this.vaildData(this.permissions.rca_swckbankcashpaymentnote_add, false),
        editBtn: this.vaildData(this.permissions.rca_swckbankcashpaymentnote_edit, false),
        //createFareBtn: this.vaildData(this.permissions.rca_swckbankcashpaymentnote_createFare, false)
      };
    },
    createFareOption(){
      return {
        emptyBtn: false,
        submitBtn: false,
        column: [ {
          "type": "date",
          "label": "开始日期",
          "prop": "startDate",
          format: 'yyyy-MM-dd',
          valueFormat: 'yyyy-MM-dd',
          rules: [
            {
              required: true,
              message: '请选择交易开始日期',
              trigger: 'blur'
            }
          ]
        }, {
          "type": "date",
          "label": "结束日期",
          "prop": "endDate",
          format: 'yyyy-MM-dd',
          valueFormat: 'yyyy-MM-dd',
          rules: [
            {
              required: true,
              message: '请选择交易结束日期',
              trigger: 'blur'
            }
          ]
        }]
      }
    }
  },
  methods: {
    cellStyle({ row, column, rowIndex,columnIndex }) {     
      if(8==columnIndex){
        if (row.diffAmount < 0) {
          return {
            color: "red",
          };
        }else if (row.diffAmount > 0) {
          return {
            color: "green",
          };
        }
      }     
    },
    getList (page, params) {
      this.tableLoading = true
      const { entryStartTime, lineCode } = this.searchForm
      const [startDate, endDate] = entryStartTime || []
      //const [startDate, endDate] = this.searchForm.entryStartTime || [];
      fetchList(Object.assign({
        current: page.currentPage,
        size: page.pageSize
      }, params, this.searchForm, {
        startDate, endDate
      })).then(response => {
        this.tableData = response.data.data.records
        this.page.total = response.data.data.total
        this.tableLoading = false
	this.$nextTick(() => {
          this.$refs.crud.doLayout();
        });
      }).catch(() => {
        this.tableLoading = false
      })
    },
    selectionChange (list) {
      this.selection = list.map(l => l.id);
    },
    handleDelete () {

    },
    handleCreateClick () {
      this.mode = 'create'
    },
    handleEditClick (record) {
      this.current = record
      this.mode = 'edit'
    },
    handlePreviewClick (record) {
      this.current = record
      this.mode = 'preview'
    },
    handleUpdate: function (row, index, done, loading) {
      putObj(row).then(data => {
        this.$message.success('修改成功')
        done()
        this.refreshChange()
      }).catch(() => {
        loading();
      });
    },
    //删除操作
    delBatchs: function () {
      if (this.$refs.crud.tableSelect.length < 1) {
        this.$message.error("请选择数据！");
        return false;
      }
      let ids = [];
      for (const table of this.$refs.crud.tableSelect) {
        ids.push(table.id);
      }
      this.$confirm("确认删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        delBatch(ids)
          .then((data) => {
            this.$message.success("删除成功");
            this.getList(this.page);
            done();
          })
          .catch(() => {
            loading();
          });
      });
    },
    handleSave: function (row, done, loading) {
      addObj(row).then(data => {
        this.$message.success('添加成功')
        done()
        this.refreshChange()
      }).catch(() => {
        loading();
      });
    },
    async searchChange (form, done) {
      this.searchForm = { ...form };
      try {
        await this.getList(this.page, form);
      } finally {
        done();
      }
    },
    refreshChange () {
      this.getList(this.page)
    },
    searchReset () {
      this.page.currentPage = 1
      this.getList(this.page);
    },
    handleClosedDialog () {
      this.mode = null
      this.refreshChange()
    },
    createFareDifference(){
      //this.formCreate = {};
      this.editVisableDialog = true;
    },
    editFare(){
      this.loading = true;
      createFares(this.formCreate)
        .then(data => {
          this.$message.success('生成成功！')
          this.editVisableDialog = false
          this.refreshChange()
          this.loading = false;
        }).catch(error=> {
          if(error.message.indexOf('timeout')!=-1){
            this.$message.warning('生成需要一定时间，请稍后查看!')
            this.editVisableDialog=false
            this.refreshChange()
            this.loading = false;
          }else{
            this.loading = false;
          }
        });
    }
  }
}
</script>
<style lang="scss" scoped>
.bottom-menu-noncash {
  margin-top: -30px;
  text-align: center;
}
</style>
